// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import store, { GAME_STATE } from "./common";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PipeGroup extends cc.Component {

    @property(cc.Node)
    topPipe: cc.Node = null;

    @property(cc.Node)
    bottomPipe: cc.Node = null;

    @property
    speed: number = 0;
    @property
    bottomYRange: cc.Vec2 = cc.v2(0, 0);
    @property
    spacingRange: cc.Vec2 = cc.v2(0, 0);

    onEnable() {
        let bottomYPos = this.bottomYRange.x + Math.random() * (this.bottomYRange.y - this.bottomYRange.x);
        let space = this.spacingRange.x + Math.random() * (this.spacingRange.y - this.spacingRange.x);
        let topYPos = bottomYPos + space;
        this.topPipe.y = topYPos;
        this.topPipe.height = cc.winSize.height - this.topPipe.y;
        this.bottomPipe.y = bottomYPos;
    }

    update(dt) {
        if (store.state == GAME_STATE.OVER) {
            return;
        }
        this.node.x += this.speed * dt;
        let disappear = this.node.getBoundingBoxToWorld().xMax < 0;
        if (disappear) {
            store.pipeManager.recyclePipe(this.node);
        }
    }
}
